package org.extremecomponents.table.view.freemarker.extend;

import java.util.List;

import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderUtils;

import freemarker.template.SimpleHash;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * @author Eccn-sshwsfc
 * easy use method in view
 */
public class MethodUtils {

	public static void initMethods(final TableModel model, SimpleHash root) {
		root.put("getImage", new TemplateMethodModel() {

			public Object exec(List arg) throws TemplateModelException {
				if (arg.size() == 1)
					return BuilderUtils.getImage(model, arg.get(0).toString());
				else
					throw new TemplateModelException("no ImagePath");
			}
		});
		
		root.put("getMessage", new TemplateMethodModel() {
		
			public Object exec(List arg) throws TemplateModelException {
				String message = null;
				if(arg.size() == 1){
					message = model.getMessages().getMessage(arg.get(0).toString());				
				}else if(arg.size() == 2){
					message = model.getMessages().getMessage(arg.get(0).toString(), (Object[])arg.get(1));
				}else{
					throw new TemplateModelException("Error MessageCode Args");
				}
				return message==null?"":message;
			}
		
		});
	}
}
